'use client';

import { useEffect, useRef } from 'react';
import { Howl } from 'howler';
import { useStore } from '@/lib/hooks/useStore';

const SoundPlayer = () => {
  const { playCongratulationSound, setPlayCongratulationSound } = useStore();
  const soundRef = useRef<Howl | null>(null);

  useEffect(() => {
    // 初始化音效
    soundRef.current = new Howl({
      src: ['/sounds/congratulation.mp3'],
      volume: 0.5,
      onend: () => {
        setPlayCongratulationSound(false);
      },
    });

    return () => {
      if (soundRef.current) {
        soundRef.current.stop();
      }
    };
  }, [setPlayCongratulationSound]);

  useEffect(() => {
    if (playCongratulationSound && soundRef.current) {
      soundRef.current.play();
    }
  }, [playCongratulationSound]);

  return null; // 这是一个无UI组件
};

export default SoundPlayer; 